import requests
import urllib.request

xx = 0

#基金代码列表
jjlist = 'http://fund.eastmoney.com/js/fundcode_search.js'
month12 = {}
month6 = {}
month3 = {}
month1 = {}

re = urllib.request.Request(jjlist)

respon = urllib.request.urlopen(re).read().decode('utf-8')
x = respon.split('=')[1].split(';')[0]
n = eval(x)
for i in n:
    xx += 1
    try:
        # 基金详细信息
        jjinfo = 'http://fund.eastmoney.com/pingzhongdata/{}.js?v=20190731135842'.format(i[0])

        re1 = urllib.request.Request(jjinfo)
        respon1 = urllib.request.urlopen(re1).read().decode('utf-8')
        shouyilv = respon1.split('/*收益率*/')[1].split('/*股票仓位测算图*/')[0].split(';')
        temp_12 = float(shouyilv[0].split('"')[1])
        temp_6 = float(shouyilv[1].split('"')[1])
        temp_3 = float(shouyilv[2].split('"')[1])
        temp_1 = float(shouyilv[3].split('"')[1])
        month12.update({i[0]: temp_12})
        month6.update({i[0]: temp_6})
        month3.update({i[0]: temp_3})
        month1.update({i[0]: temp_1})
    except Exception:
        month12.update({i[0]: 0})
        month6.update({i[0]: 0})
        month3.update({i[0]: 0})
        month1.update({i[0]: 0})
dar12 = sorted(month12.items(), key=lambda x: x[1], reverse=True)
dar6 = sorted(month6.items(), key=lambda x: x[1], reverse=True)
dar3 = sorted(month3.items(), key=lambda x: x[1], reverse=True)
dar1 = sorted(month1.items(), key=lambda x: x[1], reverse=True)

print(dar12[0:25])
print(dar6[0:25])
print(dar3[0:25])
print(dar1[0:25])